/*
 * -------------------------------------------------------------------------
 * utility.c -- utility function
 * SWT - Scilab wavelet toolbox
 * Copyright (C) 2005-2006  Roger Liu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * -------------------------------------------------------------------------
 */

#include "swt_common.h"
#include "dwt.h"
#include "parameters.h"
#include <stdlib.h>

double pBuf1[DWT_CONVOLUTION_BUFFER_1];
double pBuf2[DWT_CONVOLUTION_BUFFER_2];

void
dyaddown_1D_keep_even(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength) {
    int count = 0;
    for (count = 0; count < sigOutLength; count++) {
        sigOut[count] = sigIn[count * 2 + 1];
    }

    return;
}

void
dyadup_1D_feed_odd(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength) {
    int count = 0;
    for (count = 0; count < sigInLength - 1; count++) {
        sigOut[count * 2 + 1] = 0;
        sigOut[count * 2] = sigIn[count];
    }
    sigOut[sigOutLength - 1] = sigIn[sigInLength - 1];
    return;
}

void
dyadup_1D_feed_even(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength) {
    int count = 0;
    for (count = 0; count < sigInLength; count++) {
        sigOut[count * 2] = 0;
        sigOut[count * 2 + 1] = sigIn[count];
    }
    sigOut[sigOutLength - 1] = 0;
    return;
}

void
wextend_1D_center(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength,
        extend_method extMethod) {
    int count = 0;
    int addLength = 0;

    addLength = (sigOutLength - sigInLength) >> 1;
    for (count = 0; count < addLength; count++) {
        sigOut[count] = 0;
        sigOut[count + sigInLength + addLength] = 0;
    }

    for (count = 0; count < sigInLength; count++) {
        sigOut[count + addLength] = sigIn[count];
    }

    switch (extMethod) {
        case ZPD: break;
        case SYMH:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[addLength - count - 1];
                sigOut[count + sigInLength + addLength] =
                        sigIn[sigInLength - count - 1];
            }
            break;
        }
        case SYMW:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[addLength - count];
                sigOut[count + sigInLength + addLength] =
                        sigIn[sigInLength - count - 2];
            }
            break;
        }
        case ASYMH:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[addLength - count - 1] * (-1);
                sigOut[count + sigInLength + addLength] =
                        sigIn[sigInLength - count - 1] * (-1);
            }
            break;
        }
        case ASYMW:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[addLength - count] * (-1);
                sigOut[count + sigInLength + addLength] =
                        sigIn[sigInLength - count - 2] * (-1);
            }
            break;
        }
        case SP0:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[0];
                sigOut[count + sigInLength + addLength] =
                        sigIn[sigInLength - 1];
            }
            break;
        }
        case SP1:
        {
            for (count = (addLength - 1); count >= 0; count--) {
                sigOut[count] = sigIn[0]-(sigIn[1] - sigIn[0])*(addLength - count);
                sigOut[sigInLength + 2 * addLength - count - 1] =
                        sigIn[sigInLength - 1] - (sigIn[sigInLength - 2] - sigIn[sigInLength - 1])*(addLength - count);
            }
            break;
        }
        case PPD:
        {
            for (count = 0; count < addLength; count++) {
                sigOut[count] = sigIn[sigInLength - addLength + count];
                sigOut[count + sigInLength + addLength] = sigIn[count];
            }
            break;
        }
        default: break;
    }
    return;
}

/*-------------------------------------------
 * Signal Extraction
 *-----------------------------------------*/
void
wkeep_1D_center(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength) {
    int count = 0;
    for (count = 0; count < sigOutLength; count++)
        sigOut[count] = sigIn[(sigInLength - sigOutLength) / 2 + count];
    return;
}

void
wkeep_1D_index(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength, int first) {
    int count = 0;
    for (count = 0; count < sigOutLength; count++)
        sigOut[count] = sigIn[count + first - 1];
    return;
}

void
conv(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength,
        double *filter, int filterLength, double * pBuf) {
    int count = 0;
    int conCount = 0;

    /* initialize the buffer */
    for (count = 0; count < filterLength - 1; count++) {
        pBuf[count] = 0; /* head */
        pBuf[count + sigInLength + filterLength - 1] = 0; /* tail */
    }
    for (count = 0; count < sigInLength; count++)
        pBuf[count + filterLength - 1] = sigIn[count];

    /* convolution */
    for (count = 0; count < sigOutLength; count++) {
        sigOut[count] = 0;
        for (conCount = filterLength - 1; conCount >= 0; conCount--) {
            sigOut[count] += filter[conCount] *
                    pBuf[count + filterLength - conCount - 1];
        }
    }

    return;
}

/*-------------------------------------------
 * Convolution
 *-----------------------------------------*/
void
conv1(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength,
        double *filter, int filterLength) {
    conv(sigIn, sigInLength, sigOut, sigOutLength, filter, filterLength, pBuf1);
    return;
}

void
conv2(double *sigIn, int sigInLength,
        double *sigOut, int sigOutLength,
        double *filter, int filterLength) {
    conv(sigIn, sigInLength, sigOut, sigOutLength, filter, filterLength, pBuf2);
    return;
}

